package org.example.dao.impl;

import org.example.dao.UserDao;
import org.example.tools.JDBCTool;
import org.example.vo.Users;

import javax.validation.constraints.AssertFalse;
import javax.validation.constraints.AssertTrue;
import javax.xml.transform.Result;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class UserDaoImpl implements UserDao {

//    public static void main(String[] args) {
//        UserDao userDao = UserDao.getInstance();
//        userDao.queryUserList();
//
//    }
    @Override
    public Users userLogin(Users users) {
        try {
            Connection conn = JDBCTool.getConnection();
            if (conn == null) {
                System.out.println("数据库连接失败！");
                return null;
            }

            String sql = "select * from users where username = ? and password = ?";
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setString(1, users.getUsername());
            ps.setString(2, users.getPassword());
            ResultSet set = ps.executeQuery();
//            users = null;
            while (set.next()) {
                Integer id = set.getInt("id");
                String workCode = set.getString("work_code");
                String username = set.getString("username");
                String password = set.getString("password");
                String department = set.getString("department");
                String post = set.getString("post");
                String registerDate = set.getString("register_date");
                String gender = set.getString("gender");
                String telephone = set.getString("telephone");
                String birthDate = set.getString("birth_date");
                String email = set.getString("email");
                String role = set.getString("role");
                Integer state = set.getInt("state");
                users = new Users(id, workCode, username, password, department, post, registerDate, gender, telephone, birthDate, email, role, state);
                System.out.println(users.getUsername());
                return users;
            }
            conn.close();
//            if (users == null) {
//                System.out.println("账户和密码不正确！！！");
//            } else {
//                System.out.println("欢迎" + users.getUsername() + "，登录系统！！！");
//            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }

    @Override
    public List<Users> queryUserList(String username) {
        List<Users> list = new ArrayList<>();
//        System.out.println("nihao");
        try {
            // 获取连接对象
            Connection conn = JDBCTool.getConnection();
            StringBuilder sql = new StringBuilder("select * from users ");
            if(username!=null&&!"".equals(username)){
                sql.append(" where username like ? order by id limit 0,1");
            }
            else {
                sql.append("order by id  desc limit 0,10");
            }

            // Statement是可以执行sql语句
            PreparedStatement statement = conn.prepareStatement(sql.toString());
            if(username!=null&&!"".equals(username)){
                statement.setString(1,username);
            }
            // 执行查询，可以得到结果集
            ResultSet set = statement.executeQuery();
            // 遍历结果集合
            while (set.next()) {
                Integer id = set.getInt("id");
                String workCode = set.getString("work_code");
                username = set.getString("username");
                String password = set.getString("password");
                String department = set.getString("department");
                String post = set.getString("post");
                String registerDate = set.getString("register_date");
                String gender = set.getString("gender");
                String telephone = set.getString("telephone");
                String birthDate = set.getString("birth_date");
                String email = set.getString("email");
                String role = set.getString("role");
                Integer state = set.getInt("state");
                Users users = new Users(id, workCode, username, password, department, post, registerDate, gender, telephone, birthDate, email, role, state);
                System.out.println(users.getUsername());
                list.add(users);
            }
            // 关闭连接，释放资源
            conn.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println(list.size());
        return list;
    }

    @Override
    public int insertUser(Users user) {
        try {
            Connection conn = JDBCTool.getConnection();
            String sql = "insert into users(work_code,username,password,department,post,register_date,gender,telephone,birth_date,email) value (?,?,?,?,?,?,?,?,?,?)";
            PreparedStatement statement = conn.prepareStatement(sql);
            // 通过statement的setXX方法，完成参数的传递
            statement.setString(1, user.getWorkCode());
            statement.setString(2, user.getUsername());
            statement.setString(3, user.getPassword());
            statement.setString(4, user.getDepartment());
            statement.setString(5, user.getPost());
            statement.setString(6, user.getRegisterDate());
            statement.setString(7, user.getGender());
            statement.setString(8, user.getTelephone());
            statement.setString(9, user.getBirthDate());
            statement.setString(10, user.getEmail());
            // 完成sql语句的执行 添加、删除、更新，影响了多少行数
            int rowsNum = statement.executeUpdate();
            conn.close();
            return rowsNum;
        }catch (Exception e){
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public int deleteUser(String id) {
        try{
            Connection conn = JDBCTool.getConnection();
            String sql = "delete from users where id=?";
            PreparedStatement statement = conn.prepareStatement(sql);
            statement.setString(1, id);
            int rowsNum = statement.executeUpdate();
            conn.close();
            return rowsNum;
        }catch (Exception e){
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public Users queryUserById(String id) {

        try {
            // 获取连接对象
            Connection conn = JDBCTool.getConnection();
            String sql = new String("select * from users where id=?");
            // Statement是可以执行sql语句
            PreparedStatement statement = conn.prepareStatement(sql);
            statement.setString(1, id);
            // 执行查询，可以得到结果集
            ResultSet set = statement.executeQuery();
            Users user =null;
            // 遍历结果集合
            while (set.next()) {
//                Integer ide = set.getInt("id");
                String workCode = set.getString("work_code");
                String username = set.getString("username");
                String password = set.getString("password");
                String department = set.getString("department");
                String post = set.getString("post");
                String registerDate = set.getString("register_date");
                String gender = set.getString("gender");
                String telephone = set.getString("telephone");
                String birthDate = set.getString("birth_date");
                String email = set.getString("email");
                String role = set.getString("role");
                Integer state = set.getInt("state");
                user =new Users(set.getInt("id"), workCode, username, password, department, post, registerDate, gender, telephone, birthDate, email, role, state);
            }
            conn.close();
            return user;
        }catch (Exception e) {
        e.printStackTrace();
    }
        return null;
    }

    @Override
    public int updateUser(Users users) {

        try {
            String sql = "update users set work_code=?,username=?,password=?,department=?,post=?,gender=?,telephone=?,email=? where id=?";
            Connection conn = JDBCTool.getConnection();
            PreparedStatement statement = conn.prepareStatement(sql);
            statement.setString(1,users.getWorkCode());
            statement.setString(2,users.getUsername());
            statement.setString(3,users.getPassword());
            statement.setString(4,users.getDepartment());
            statement.setString(5,users.getPost());
            statement.setString(6,users.getGender());
            statement.setString(7,users.getTelephone());
            statement.setString(8,users.getEmail());
            statement.setInt(9,users.getId());
            int rows = statement.executeUpdate();
            conn.close();
            return rows;
        }catch (Exception e){
            e.printStackTrace();
        }
        return 0;
    }
}
